#pragma once

#include <string>
#include <memory>
#include <unordered_map>
#include <ctime>
#include <unistd.h>

class Session
{
public:
    Session(const std::string& username, const std::string& status)
        :_username(username), _status(status)
    {}
    ~Session()
    {}

    std::string GetName()
    {
        return _username;
    }

private:
    std::string _username;
    std::string _status;
    uint64_t _create_time;
};

using session_ptr = std::shared_ptr<Session>;

class SessionManager
{
public:
    SessionManager()
    {
        srand(time(nullptr)^getpid());
    }

    std::string Insert(session_ptr sp)
    {
        std::string userid;
        do
        {
            userid = std::to_string(time(nullptr)+rand());
        } while (_sessions.count(userid));
        _sessions.insert(std::make_pair(userid, sp));
        return userid;
    }

    session_ptr GetSession(std::string sessionid)
    {
        if(_sessions.count(sessionid) == 0) return nullptr;
        return _sessions[sessionid];
    }
private:
    std::unordered_map<std::string, session_ptr> _sessions;
};